﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace WireLessPC
{
    public partial class mainform : Form
    {
        Socket socket = null;
        Boolean connected = false;
        public mainform()
        {
            InitializeComponent();
        }

        private void mainform_Load(object sender, EventArgs e)
        {
            loadIp();
            loadPort();

        }

        private void loadIp()
        {
            IPHostEntry IpEntry = Dns.GetHostEntry(Dns.GetHostName());
            IPAddress[] addressList = IpEntry.AddressList;
            if (addressList != null)
            {
                for (int i = 0; i < addressList.Length; i++)
                {
                    if (!addressList[i].IsIPv6LinkLocal && addressList[i] != null && addressList[i].ToString().Length>0)
                    {
                        ddip.Items.Add(addressList[i].ToString());
                    }
                }
            }
            ddip.Text = ddip.Items.Count >0 ? "请选择一个地址" : "没有检测到IP";
        }
        private void loadPort()
        {
            this.tbcmdport.Text = "1987";
            this.tbscreenport.Text = "1987";
        }

        private void minToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Visible = false;
            this.ninotification.Visible = true;
        }

        private void bstart_Click(object sender, EventArgs e)
        {
            //startService();
            //if (connected)
            //{
            //    this.pbimage.ImageLocation = "D:\\WirelessPC\\WireLessPC\\WireLessPC\\ico\\correct.png";
            //    this.lstatus.Text = "您已经建立了到服务器的链接!";
            //    this.bstart.Enabled = false;
                //acceptanceSocket();
                System.Threading.Thread thread = new System.Threading.Thread(new ThreadStart(BeginListen));
                thread.Start();
            //}
        }
        private void startService()
        {
            try
            {
                if (this.ddip.Text != null && this.ddip.Text.Length > 0 && this.ddip.Text.IndexOf(".") > 0)
                {
                    IPAddress ipaddress = IPAddress.Parse(this.ddip.Text.ToString().Trim());
                    int cmdPort = -1;
                    if (this.tbcmdport.Text != null && this.tbcmdport.Text.ToString().Length > 0)
                    {
                        try
                        {
                            cmdPort = int.Parse(this.tbcmdport.Text.ToString().Trim());
                        }
                        catch (Exception e)
                        {
                            if ("OK".ToLower() == System.Windows.Forms.MessageBox.Show("命令端口" + this.tbcmdport.Text.ToString() + "无效，系统将指定1555端口!", "端口无效", MessageBoxButtons.OKCancel).ToString().ToLower())
                            {
                                this.tbcmdport.Text = "1555";
                                cmdPort = 1555;
                            }
                        }
                    }
                    if (cmdPort != -1)
                    {
                        IPEndPoint endpoint = new IPEndPoint(ipaddress, cmdPort);
                        socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                        //SocketInformation socketinfo = new SocketInformation();

                        socket.Bind(endpoint);
                        socket.Listen(20);
                        connected = true;
                    }
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show("请选择一个IP地址，在链接之前!");
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message.ToString());
            }
        }
        private void mainform_Resize(object sender, System.EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Visible = false;
                this.ninotification.Visible = true;
            }
        }
        private void acceptanceSocket()
        {
            try
            {
                if (socket != null)
                {
                    Socket acceptance = socket.Accept();
                    //while (true)
                    // {
                    MessageBox.Show("anything");
                    //byte[] received = new byte[100];
                    //acceptance.Receive(received);
                    //MessageBox.Show(System.Text.Encoding.UTF8.GetString(received));
                    //System.Threading.Thread.Sleep(2000);
                    // }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message.ToString());
            }
        }
        private void BeginListen()
        {
            IPAddress ServerIp = IPAddress.Parse("192.168.1.102");
            IPEndPoint iep = new IPEndPoint(ServerIp, 1988);
            socket = new
                       Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            byte[] byteMessage = new byte[8192];
            socket.Bind(iep);
            while (true)
            {
                try
                {
                    socket.Listen(5);
                    Socket newsocket = socket.Accept();
                    MessageBox.Show("anything");
                    //newsocket.Receive(byteMessage);
                    //string sTime = DateTime.Now.ToShortTimeString();
                }
                catch (SocketException ex)
                {
                }
            }

        }
        private void ninotification_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Visible = true;
            this.ninotification.Visible = true;
        }
    }
}
